package com.zhihui.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil extends Properties {

	private static final long serialVersionUID = 50440463580273222L;

	private static PropertyUtil instance = null;

	public static synchronized PropertyUtil getInstance(String ProName) {
		if (instance == null) {
			instance = new PropertyUtil(ProName);
		}
		return instance;
	}

	public String getProperty(String key, String defaultValue) {
		String val = getProperty(key);
		return (val == null || val.isEmpty()) ? defaultValue : val;
	}

	public String getString(String name, String defaultValue) {
		return this.getProperty(name, defaultValue);
	}

	public int getInt(String name, int defaultValue) {
		String val = this.getProperty(name);
		return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);
	}

	public long getLong(String name, long defaultValue) {
		String val = this.getProperty(name);
		return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);
	}

	public float getFloat(String name, float defaultValue) {
		String val = this.getProperty(name);
		return (val == null || val.isEmpty()) ? defaultValue : Float.parseFloat(val);
	}

	public double getDouble(String name, double defaultValue) {
		String val = this.getProperty(name);
		return (val == null || val.isEmpty()) ? defaultValue : Double.parseDouble(val);
	}

	public byte getByte(String name, byte defaultValue) {
		String val = this.getProperty(name);
		return (val == null || val.isEmpty()) ? defaultValue : Byte.parseByte(val);
	}

	private PropertyUtil() {
	}

	public PropertyUtil(String ProName) {
		try {
			InputStream in = this.getClass().getClassLoader().getResourceAsStream(ProName);

			this.load(in);
			in.close();
		} catch (IOException e) {
			System.out.println("配置文件读取异常," + e.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
